<?php  
 
class News extends Model {
// ������

    function __construct(){ 
       
      
    }
    
    function getNew($news_id){
        $q = "SELECT news.* FROM news 
            WHERE news_id = $news_id";
        $r = mysql_query($q);
          if(!$r) return -1;
        if( mysql_num_rows($r) > 0){
            $tmp = mysql_fetch_assoc($r);
            return $tmp;
        }
        return false;
    
    }
    
    function getNews($year = false){ 
        
          if($year === false){
            //TODO ����� ��� ����
            return;
          }
    
         $q = "SELECT news.*,
              EXTRACT(YEAR FROM news.date_added) AS year
              FROM news 
              HAVING year = '$year' ";
          $r = mysql_query($q);
          if(!$r) return -1;
         if( mysql_num_rows($r) > 0){
            while($tmp = mysql_fetch_assoc($r)){
                  
                $tmp['photo_sm']  = "sm_".$tmp['photo'].".jpg";
                $tmp['link'] = "/news/ShowNew/".$tmp['news_id'];
                $arr[] = $tmp;   
            }     
            return $arr;
          } 
          return false;
    } // --
    
    function addNew($new){  
    
        $condition = '';
        $photo = " `photo` =   ";
        $mess = '';
        
        foreach($new as $option => $value){ 
            if($option == 'year') continue;
            $condition .= " $option = '$value', "; 
        }
        
       if(isset($_FILES['photo']['name'])){ // name type tmp_name error size
       
          $c = count($_FILES['photo']['name']);
          $photo .= "'";
          
           
              $old_name = $_FILES['photo']['tmp_name'] ;
              $img_name = substr(md5(md5($old_name . date("m.d.y"))),0,8) ;
              $tmb = "sm_" . $img_name. ".jpg";
              $big = $img_name. ".jpg";
              
              $photo .=  $img_name ; 
              
              $path_1 = $_SERVER['DOCUMENT_ROOT'] . "../Image/news/$tmb";
              $path_2 = $_SERVER['DOCUMENT_ROOT'] . "../Image/news/$big";
              
              $res_1 = $this->imgCropAndResize($old_name, $path_1, 300);
              $res_2 = $this->imgCropAndResize($old_name, $path_2, 600);
              
              if($res_1 == -100) $mess = '�������� ����� ���������';
           
          $photo .= "'";
          
       } else $photo .= "' '"; 
         
      $q = "INSERT INTO `news` SET  $condition $photo, date_added	= '" . date("Y-m-d") . "'"; fb($q);
      $r = mysql_query($q);
      if(!$r) return -1;
       
       
        
        return false;
    }
    
  
    
    
}




    
 
?>